package com.sqfw.project.wpReport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import com.sqfw.project.wpReport.domain.SeatEvaluateReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.VisitWorkReport;
import com.sqfw.project.wpReport.service.IVisitWorkReportService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 回访组工作情况汇总表Controller
 * 
 * @author lx
 * @date 2023-07-28
 */
@RestController
@RequestMapping("/wpReport/visitWorkReport")
public class VisitWorkReportController extends BaseController
{
    @Autowired
    private IVisitWorkReportService visitWorkReportService;

    /**
     * 查询回访组工作情况汇总表列表
     */
    @PreAuthorize("@ss.hasPermi('getVisitWorkReportList')")
    @GetMapping("/getVisitWorkReportList")
    public TableDataInfo getVisitWorkReportList(VisitWorkReport visitWorkReport)
    {
        startPage();

        TableDataInfo tableDataInfo = visitWorkReportService.getVisitWorkReportList(visitWorkReport);

        long total = tableDataInfo.getTotal();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        if ((long) pageNum * pageSize >= total && total > 0) {
            List<VisitWorkReport> list = (List<VisitWorkReport>) tableDataInfo.getRows();

            if (list.size()>0){
                TableDataInfo tableDataInfoCon = visitWorkReportService.getVisitWorkReportList(visitWorkReport);
                List<VisitWorkReport> reportList = (List<VisitWorkReport>) tableDataInfoCon.getRows();
                VisitWorkReport report = new VisitWorkReport();

                report.setCode("总计");
                //成功回访
                int sucessVisitCount = reportList.stream()
                        .map(VisitWorkReport::getSucessVisitCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setSucessVisitCount(sucessVisitCount);
                //拒绝回访
                int refuseVisitCount = reportList.stream()
                        .map(VisitWorkReport::getRefuseVisitCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setRefuseVisitCount(refuseVisitCount);
                //无需回访
                int visitNoCount = reportList.stream()
                        .map(VisitWorkReport::getVisitNoCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setVisitNoCount(visitNoCount);
                //回访总数
                int visitTotalCount = reportList.stream()
                        .map(VisitWorkReport::getVisitTotalCount)
                        .reduce(Integer::sum)
                        .orElse(0);
                report.setVisitTotalCount(visitTotalCount);


                list.add(report);
            }

        }
        return tableDataInfo;
    }

    /**
     * 导出回访组工作情况汇总表列表
     */
    @Log(title = "回访组工作情况汇总表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('getVisitWorkReportList:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, VisitWorkReport visitWorkReport)
    {
        TableDataInfo tableDataInfo = visitWorkReportService.getVisitWorkReportList(visitWorkReport);
        List<VisitWorkReport> list = (List<VisitWorkReport>) tableDataInfo.getRows();

        if (list.size()>0){
            VisitWorkReport report = new VisitWorkReport();

            report.setCode("总计");
            //成功回访
            int sucessVisitCount = list.stream()
                    .map(VisitWorkReport::getSucessVisitCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setSucessVisitCount(sucessVisitCount);
            //拒绝回访
            int refuseVisitCount = list.stream()
                    .map(VisitWorkReport::getRefuseVisitCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setRefuseVisitCount(refuseVisitCount);
            //无需回访
            int visitNoCount = list.stream()
                    .map(VisitWorkReport::getVisitNoCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setVisitNoCount(visitNoCount);
            //回访总数
            int visitTotalCount = list.stream()
                    .map(VisitWorkReport::getVisitTotalCount)
                    .reduce(Integer::sum)
                    .orElse(0);
            report.setVisitTotalCount(visitTotalCount);


            list.add(report);

        }
        ExcelUtil<VisitWorkReport> util = new ExcelUtil<VisitWorkReport>(VisitWorkReport.class);
        util.exportExcel(response, list, "回访组工作情况汇总表数据");
    }

}
